//
//  CALayer+Extension.swift
//  Sevengrass
//
//  Created by Kipp Dev on 2017/12/14.
//  Copyright © 2017年 Syrena. All rights reserved.
//

import Foundation

enum GradientDirection {
    case vertical
    case horizon
}

// MARK: - 给layer添加一个渐变背景,不能给用了约束的空间添加，无法获取到正确的frame
extension CALayer {
    func getGradientLayer(direction:GradientDirection,colors:[UIColor]) {
        insertSublayer(CAGradientLayer().then {
            $0.colors = colors.map {$0.cgColor}
            $0.startPoint =  CGPoint(x: 0, y: 0)
            $0.endPoint = direction == .horizon ? CGPoint(x: 1, y: 0) : CGPoint(x: 0, y: 1)
            $0.frame = bounds
        }, below: sublayers?.first)
    }
}

extension UIView {

    /// 部分圆角
    ///
    /// - Parameters:
    ///   - corners: 需要实现为圆角的角，可传入多个
    ///   - radii: 圆角半径
    func corner(byRoundingCorners corners: UIRectCorner, radii: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
    }
}
